﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EntitiesLayer;
using System.Transactions;
using System.Data.SqlClient;
using System.Data;
using DAL.Repositories;
using DAL.AbstractRepositories;

namespace DAL.Repositorios
{
    public class PagoRepository : DAL.AbstractRepositories.AbstractRepository<Pago>
    {

        public override void Insert(Pago entity)
        {
            Inserted.Add(entity);
        }

        public override IEnumerable<Pago> GetAll()
        {
            SqlCommand cmd = new SqlCommand();
            DataSet ds = DBAccess.ExecuteSPWithDS(ref cmd, "PaListarPagos");

            foreach (DataRow dr in ds.Tables[0].Rows)
                yield return crearPago(Convert.ToInt32(dr["IdPago"]), dr);

        }

        public override Pago GetById(int id)
        {
            throw new NotImplementedException();
        }
        internal static IList<Pago> PagoCliente(int idCliente) {


            IList<Pago> listaPago = new List<Pago>();
            SqlCommand cmd = new SqlCommand();
            cmd.Parameters.Add(new SqlParameter("@IdCliente", idCliente));
            DataSet ds = DBAccess.ExecuteSPWithDS(ref cmd, "PaConsultarPagosCliente");

            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                Pago p = crearPago(Convert.ToInt32(dr["IdPago"]), dr);
                listaPago.Add(p);
            }

            return listaPago;
        }

        internal static Pago crearPago(int id, DataRow dr) {

            return new Pago(
                idPago: id,
                montoPagado: Convert.ToDecimal(dr["MontoPagado"]),
                tipo: (TipoPago)Convert.ToChar(dr["Tipo"]),
                fechaDePago: Convert.ToDateTime(dr["FechaPago"]),
                diasEfectivos: Convert.ToInt32(dr["DiasEfectivos"]),
                idCliente: Convert.ToInt32(dr["IdUsuario"])
                
                );
        }

        public System.Data.DataSet GetListaUsuariosPagar()
        {
            SqlCommand cmd = new SqlCommand();
            DataSet ds = DBAccess.ExecuteSPWithDS(ref cmd, "PaListarUsuariosOpti");
            return ds;
        }

        protected override void InsertEntity(Pago objPago)
        {

                //public Pago(int idCliente ,decimal montoPagado, DateTime fechaDePago, TipoPago tipo, int diasEfectivos, int idPago=0)

                SqlCommand cmd = new SqlCommand();
                cmd.Parameters.Add(new SqlParameter("@IdCliente", objPago.IdCliente));
                cmd.Parameters.Add(new SqlParameter("@MontoPagado", objPago.MontoPagado));
                cmd.Parameters.Add(new SqlParameter("@FechaDePago", objPago.FechaDePago));
                cmd.Parameters.Add(new SqlParameter("@TipoPago", (char) objPago.Tipo));
                cmd.Parameters.Add(new SqlParameter("@DiasEfectivos", objPago.DiasEfectivos));
                DataSet ds = DBAccess.ExecuteSPWithDS(ref cmd, "PaRegistrarPago");
            
        }
    }
}
